const func = (arr, l, r) => {
  if (l >= r) {
    return;
  }

  let mid = Math.floor((l + r) / 2);
  func(arr, l, mid);
  func(arr, mid + 1, r);
  let index = 0;
  let temp = [];
  let x = l,
    y = mid + 1;
  while (x <= mid && y <= r) {
    if (arr[x] < arr[y]) {
      temp.push(arr[x]);
      x++;
    } else if (arr[x] >= arr[y]) {
      temp.push(arr[y]);
      y++;
    }
  }

  while (x <= mid) {
    temp.push(arr[x]);
    x++;
  }

  while (y <= r) {
    temp.push(arr[y]);
    y++;
  }

  for (let i = l; i <= r; i++) {
    arr[i] = temp[index++];
  }
};

let arr = [4, 3, 2, 6, 4, 2];

func(arr, 0, arr.length - 1);

console.log(arr);
